package com.geostar.constructionland.analyse.factory;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.geostar.constructionland.analyse.config.SpringUtil;
import com.geostar.constructionland.analyse.entity.Block;
import com.geostar.constructionland.analyse.entity.alyresult.AlyResult;
import com.geostar.constructionland.analyse.entity.alyresult.tzhdlgdfx.AlyResultGdZlDb;
import com.geostar.constructionland.analyse.entity.model.AnalyseModel;
import com.geostar.constructionland.analyse.entity.statistics.SticDlfxTzh;
import com.geostar.constructionland.analyse.mapper.AdjustVersionMapper;
import com.geostar.constructionland.analyse.service.analyse.impl.Analyse;
import com.geostar.constructionland.common.entity.biz.JsydDlbm;
import com.geostar.constructionland.common.entity.response.AdjustAgrVersion;
import com.geostar.constructionland.common.entity.response.LandInfo;
import com.geostar.constructionland.common.entity.response.model.AdjustModel;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author yelll
 * @since 2020-6-1 17:43
 **/
@Component
public class AdjustModelFactory {

    public static AdjustModel getAdjustModByAnalyse(AnalyseModel analyseModel) throws Exception{
        AdjustModel adMoel = new AdjustModel();
        adMoel.setYwid(analyseModel.getYwid());
        adMoel.setFxbs(analyseModel.getAlyId());
        QueryWrapper adjustWrapper = new QueryWrapper<AdjustAgrVersion>().eq("adjust_type",analyseModel.getFxlxMapped());
        /**参照平差服务*/
        if(StringUtils.isBlank(analyseModel.getAdjustVersion())){
            /**查找最新的内容*/
            adjustWrapper.orderByDesc("adjust_version");
        }else {
            adjustWrapper.eq("adjust_version",analyseModel.getAdjustVersion());
        }
        AdjustVersionMapper adjustVersionMapper = SpringUtil.getBean(AdjustVersionMapper.class);
        List<AdjustAgrVersion> adjustAgrVersions = adjustVersionMapper.selectList(adjustWrapper);
        if(CollectionUtils.isNotEmpty(adjustAgrVersions)){
            adMoel.setAdjustVersion(adjustAgrVersions.get(0).getAdjustVersion());
            adMoel.setAdjustAgrVersion(adjustAgrVersions.get(0));
        }else {
            throw new Exception("找不到平差版本信息");
        }
        return adMoel;
    }


    /***
     * 普通平差方法
     * @param analyseModel
     * @param alyResult
     * @return
     * @throws IOException
     */
    public static AdjustModel getAdjustMod(AnalyseModel analyseModel, AlyResult alyResult) throws IOException {
        AdjustModel adjustModel = new AdjustModel();
        AdjustVersionMapper adjustVersionMapper = SpringUtil.getBean(AdjustVersionMapper.class);
        List<AdjustAgrVersion> adjustAgrVersions = adjustVersionMapper.selectList(new QueryWrapper<AdjustAgrVersion>().eq("adjust_type",analyseModel.getFxlxMapped()));
        adjustModel.setAdjustVersion(analyseModel.getAdjustVersion());
        adjustModel.setAdjustAgrVersion(adjustAgrVersions.get(0));
        adjustModel.setAnalyseType(analyseModel.getFxlx());
        ObjectMapper objectMapper = new ObjectMapper();
        /**准备数据**/
        List<Map> alyResultXzqs = objectMapper.readValue(alyResult.getAnalysisResult().getResultRecords(),
            new TypeReference<List<Map>>(){});
        /**地块信息**/
        List<Block> blocks = analyseModel.getBlocks();

        /**地块面积的和的四舍五入**/
        BigDecimal sum_dkmj = blocks.stream().map(Block::getDkmj_ys).reduce(BigDecimal.ZERO,BigDecimal::add).setScale(0,BigDecimal.ROUND_HALF_UP);
        /**占压信息**/
        List<LandInfo> landInfos = new ArrayList<>();
        alyResultXzqs.stream().forEach(map -> {
            LandInfo landInfo = new LandInfo();
            landInfo.setFxId(map.get("FXID").toString());
            landInfo.setZymj_ys(Double.valueOf(map.get("YZMJ").toString()));
            landInfo.setDkmj_ys(Double.valueOf(map.get("DK_MJ").toString()));
            landInfo.setDkid(map.get("DK_ID").toString());
            landInfos.add(landInfo);
        });

        /**遍历占压面积四舍五入**/
        landInfos.forEach(landInfo -> {
            double zymj = new BigDecimal(landInfo.getZymj_ys()).setScale(0,BigDecimal.ROUND_HALF_UP).doubleValue();
            landInfo.setZymj(zymj);
            landInfo.setZymj_adjusted(zymj);
            BigDecimal zymj_ys_bd_round_down =  new BigDecimal(landInfo.getZymj_ys());
            zymj_ys_bd_round_down = zymj_ys_bd_round_down.setScale(1,BigDecimal.ROUND_DOWN);
            BigDecimal zymj_ys_bd_scale =  new BigDecimal(landInfo.getZymj_ys());
            zymj_ys_bd_scale = zymj_ys_bd_scale.setScale(0,BigDecimal.ROUND_HALF_UP);
            double disBtwZymjDis = (zymj_ys_bd_round_down.subtract(zymj_ys_bd_scale)).doubleValue();
            landInfo.setDisBtwZymjDis(disBtwZymjDis);
        });
        /**占压面积四舍五入值的和**/
        BigDecimal sum_zymj = new BigDecimal(landInfos.stream().collect(Collectors.summingDouble(LandInfo::getZymj)));
        adjustModel.setAdjustTotal(sum_zymj.subtract(sum_dkmj).doubleValue());
        adjustModel.setLandInfos(landInfos);
        return adjustModel;
    }

    /***
     * 调整后地类平差
     * @param list
     * @return
     */
    public static AdjustModel getAdjustModByStcDLfxTzh(List<SticDlfxTzh> list,double adjustTotal,AnalyseModel analyseModel){
        List<LandInfo> landInfos = new ArrayList<>();
        list.forEach(sticDlfxTzh -> {
            LandInfo landInfo = new LandInfo();
            landInfo.setFxId(sticDlfxTzh.getFxid());
            landInfo.setZymj_ys(sticDlfxTzh.getZymjYs());
            landInfo.setPcxh(StringUtils.isBlank(sticDlfxTzh.getPcxh())?0:Double.valueOf(sticDlfxTzh.getPcxh()));
            landInfos.add(landInfo);
        });
        AdjustModel adjustModel = new AdjustModel();
        adjustModel.setLandInfos(landInfos);
        adjustModel.setAdjustTotal(adjustTotal);
        adjustModel.setAnalyseType(analyseModel.getFxlxMapped());
        adjustModel.setAdjustVersion(analyseModel.getAdjustVersion());
        return adjustModel;
    }


    /**
     * 调整后耕地平差
     * @param alyResultGdZlDbs
     * @param adjustTotal
     * @param analyseModel
     * @return
     */
    public static AdjustModel getAdjustModByGdzlDbList(List<AlyResultGdZlDb> alyResultGdZlDbs,double adjustTotal,AnalyseModel analyseModel){
        List<LandInfo> landInfos = alyResultGdZlDbs.stream().map(a -> {
            LandInfo landInfo = new LandInfo();
            landInfo.setFxId(a.getFxid());
            landInfo.setZymj_ys(a.getZymj());
            return landInfo;
        }).collect(Collectors.toList());
        return null;
    }
}
